<template>
	<view class="content">
		<heade :title="title" cross bg_color="bg_fff"></heade>
		<view>
			<view class="m-t10">
				<view class="d-flex align-center justify-content-space-between f-s14 m-b8">
					<view class="m-l10">共{{SaleOrderDetails.length}}项</view>
					<view class="m-r10 co_FF0000">￥{{Number(total).toFixed(2)}}</view>
				</view>
				<view class="f-s14 bg_fff p-b10 p-r15 p-t10 p-l15 align-center flex1 justify-content-space-between m-t5" v-for="(item2,i) in SaleOrderDetails">
					<view class="d-flex align-center justify-content-space-between">
						<view class="d-flex">
							<view class="f-s14 f-wbold webkit-line-clamp-1" style="max-width: 580rpx;" v-if="item2.Inventory">[{{item2.Inventory.Code}}] <text class="m-l5">{{item2.Name}}</text>	</view>
							<view class="zengClass f-s12 d-flex-center co_fff m-l5" v-if="item2.isPresent">赠</view>
						</view>
						<view class="iconfont icon-shanchu" @tap="deleteClick(i)"></view>
					</view>
					<view class="m-t10">
						<view class="co_666">金额：<text class="co_E04C46 f-wbold">{{Number(item2.inall).toFixed(2)}}</text> </view>
						<view class="d-flex align-center m-t10">
							<view class="f-s13 d-flex align-items-center" @tap.stop="">
								<view class="co_000 f-wbold">单价:</view>
								<view class="p-l5" style="width: 170rpx;">
									<input type="digit" class="f-s14 p-l10 box_box d-flex-center" style="border-bottom: 2rpx solid #d4d0d0;height: 40rpx;" @blur="unitPrice($event,item2)" v-model="item2.OrigTaxPrice">
								</view>
							</view>
							<view class="f-s13 d-flex align-items-center m-l30 position-relative" @tap.stop="">
								<view class="m-l3 position-absolute" style="color: red;top: 60%;transform: translate(0,-50%);left: 6rpx;">*</view>
								<view class="co_000 f-wbold m-l15">数量:</view>
								<view class="p-l5" style="width: 170rpx;">
									<input type="digit" class="f-s14 p-l10 box_box d-flex-center" style="border-bottom: 2rpx solid #d4d0d0;height: 40rpx;" @blur="quantity($event,item2)" v-model="item2.Quantity">
								</view>
								<view class="co_000" v-if="item2.Unit">{{item2.Unit.Name}}</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="b-liuhai" ><view style="height: 90rpx;"></view></view>
		<view style="position: fixed; bottom: 0;width: 100%;z-index: 9;" class="bg_fff">
			<view class="b-liuhai">
				<view class="d-flex align-center justify-content-center">
					<view class="bg_fff d-flex-center" style="width: 25%;height: 80rpx;" v-if="title=='草稿详情'" @tap="xuancunhuo">选存货</view>
					<view class="bg_fff d-flex-center" :style="{'width':title=='草稿详情'?'25%':'50%'}" style="height: 80rpx;border-left: 2rpx solid #eee;" @tap="TakePrice">取价</view>
					<view class="co_fff d-flex-center bg_3A8CFF  f-w400" @tap="submit" style="width: 50%;height: 80rpx;">保存</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				SaleOrderDetails:[],
				content:{},
				count:0,
				arrList:[],
				total:0,
				title:'草稿详情',
				list:{},
				merge:false,
				externalCode:''
			}
		},
		onLoad(e) {
			let _this = this
			uni.$once('page-popup', (data) => {
				console.log(data)
				if(data.type == 1) {
					this.merge = data.merge
					if(data.count == 0) {
						this.title = '销售购物车'
						this.count = data.count
						this.content = data.content
						this.externalCode = data.externalCode
						this.list = this.$db.get('shoppingTrolley')
						this.list.dto.saleOrderDetails.forEach(e => {
							e.inall = Number(e.OrigTaxPrice)* Number(e.Quantity)
							this.total = (this.total + Number(e.inall))
						})
						this.SaleOrderDetails = this.list.dto.saleOrderDetails
					}else if(data.count == 2) {
						this.title = '退货购物车'
						this.count = data.count
						this.content = data.content
						this.externalCode = data.externalCode
						this.list = this.$db.get('shoppingTrolley_1')
						this.list.dto.saleDeliveryDetails.forEach(e => {
							e.inall = Number(e.OrigTaxPrice) * (-Number(e.Quantity))
							this.total = (this.total + Number(e.inall))
						})
						this.SaleOrderDetails = this.list.dto.saleDeliveryDetails
					}
					
				}else {
					if(data.count == 0) {
						this.externalCode = data.externalCode
						this.content = data.content
						data.content.saleOrderDetails.forEach(e => {
							e.inall = Number(e.origTaxPrice)* Number(e.quantity)
							this.total = (this.total + Number(e.inall))
						})
						_this.count = data.count
						this.SaleOrderDetails = data.content.saleOrderDetails
					}else {
						this.externalCode = data.externalCode
						this.content = data.content
						data.content.saleDeliveryDetails.forEach(e => {
							e.inall = Number(e.origTaxPrice)*  (-Number(e.quantity))
							this.total = (this.total + Number(e.inall))
						})
						_this.count = data.count
						this.SaleOrderDetails = data.content.saleDeliveryDetails
					}
					
				}
			})
			
		},
		methods: {
			// 选存货
			xuancunhuo() {
				let _this = this
				let title = ''
				let client = {
					name: this.content.linkMan,
					arBalance: this.content.arBalance,
					partnerAddresDTOs:[{shipmentAddress:this.content.address}],
					saleDepartment: this.content.saleDepartment,
					settlementPartner:this.content.settlementPartner
				}
				if(this.count == 0) {
					title = '销售订单'
					uni.navigateTo({
						url: `/pages/paceOrder/paceOrder`,
						success: function(res) {
							setTimeout(() => {
								uni.$emit('inventory', {
									client: client,
									title:title,
									SaleOrderDetails:_this.SaleOrderDetails,
									externalCode:_this.externalCode,
									content:_this.content
								})
							}, 500)
						}
					})
				}else if(this.count == 2){
					title = '退货单'
					uni.navigateTo({
						url: `/pages/paceOrder/returnGoods`,
						success: function(res) {
							setTimeout(() => {
								uni.$emit('inventory', {
									client: client,
									title:title,
									SaleOrderDetails:_this.SaleOrderDetails,
									externalCode:_this.externalCode,
									content:_this.content
								})
							}, 500)
						}
					})
				}
			},
			// 单价
			unitPrice(e,item) {
				if(this.count == 0) {
					if(e.detail.value <= 0) {
						item.OrigTaxPrice = 1
					}else {
						this.total -= item.inall
						item.OrigTaxPrice = e.detail.value
					}
					item.inall = Number(item.OrigTaxPrice) * Number(item.Quantity)
					this.total += item.inall
				}else if(this.count == 2) {
					if(e.detail.value <= 0) {
						item.OrigTaxPrice = 1
					}else {
						this.total -= item.inall
						item.OrigTaxPrice = e.detail.value
					}
					item.inall = Number(item.Quantity) * (-Number(item.OrigTaxPrice))
					this.total += item.inall
				}
				
				this.$forceUpdate()
			},
			// 数量
			quantity(e,item) {
				if(this.count == 0) {
					if(e.detail.value <= 0) {
						item.Quantity = 1
					}else {
						this.total -= item.inall
						item.Quantity = Number(e.detail.value)
					}
					item.inall = Number(item.Quantity) * Number(item.OrigTaxPrice)
					this.total += item.inall
				}else if(this.count == 2) {
					if(e.detail.value >= 0) {
						item.Quantity = -1
					}else {
						this.total -= item.inall
						item.Quantity = Number(e.detail.value)
					}
					item.inall = (-Number(item.Quantity)) * Number(item.OrigTaxPrice)
					this.total += item.inall
				}
				this.$forceUpdate()
			},
			// 取价
			TakePrice() {
				let _this = this
				let list = []
				this.SaleOrderDetails.forEach((k) => {
					list.push({
						invCode:k.Inventory.Code,
						partnerCode:this.content.code,
						unitName:k.Unit.Name
					})
				})
				let resData = list
				this.$http.post('ApiSA/GetNewSalePrices',resData,true).then(res => {
					if(res.statusCode = 200) {
						if(res.data) {
							uni.showToast({
								title: '取价成功',
								duration: 500
							});
							if(res.data.length > 0) {
								res.data.forEach(e => {
									_this.total = 0
									this.SaleOrderDetails.forEach((k) => {
										if(e.invCode == k.Inventory.Code && e.unitName == k.Unit.Name) {
											k.OrigTaxPrice = e.taxPrice
											if(_this.count == 0) {
												k.inall = Number(k.OrigTaxPrice) * Number(k.Quantity)
											}else if(_this.count == 2) {
												k.inall = Number(k.OrigTaxPrice) * (-Number(k.Quantity))
											}
										}
										_this.total += Number(k.inall)
									})
								})
							}
						}
					}
				})
			},
			// 删除
			deleteClick(i) {
				this.SaleOrderDetails.splice(i,1)
				this.list.dto.saleOrderDetails = this.SaleOrderDetails
				this.total = 0
				this.SaleOrderDetails.forEach((k) => {
					this.total += (Number(k.OrigTaxPrice) * Number(k.Quantity))
				})
				if(this.count == 0) {
					this.$db.set('shoppingTrolley',this.list)
				}else if(this.count == 2) {
					this.$db.set('shoppingTrolley_1',this.list)
				}
			},
			submit() {
				if(this.title.indexOf('购物车') != -1) {
					if(this.SaleOrderDetails.length > 0) {
						if(this.count == 0) {
							this.list.dto.saleOrderDetails = this.SaleOrderDetails
							if(this.merge == true) {
								let list2 = this.$db.get('dindan') 
								list2.forEach(e => {
									if(e.dto.externalCode == this.externalCode) {
										e.dto.saleOrderDetails = this.list.dto.saleOrderDetails
									}
								})
								this.$db.set('dindan',list2)
							}else {
								if(this.$db.get('dindan')) {
									let list2 = this.$db.get('dindan')
									list2.push(this.list)
									this.$db.set('dindan',list2)
								}else{
									this.$db.set('dindan',[this.list])
								}
							}
							this.$db.del('shoppingTrolley')
						}else if(this.count == 2) {
							this.list.dto.saleDeliveryDetails = this.SaleOrderDetails
							if(this.merge == true) {
								let list2 = this.$db.get('dindan_1') 
								list2.forEach(e => {
									if(e.dto.externalCode == this.externalCode) {
										e.dto.saleDeliveryDetails = this.list.dto.saleDeliveryDetails
									}
								})
								this.$db.set('dindan_1',list2)
							}else {
								if(this.$db.get('dindan_1')) {
									let list2 = this.$db.get('dindan_1')
									list2.push(this.list)
									this.$db.set('dindan_1',list2)
								}else{
									this.$db.set('dindan_1',[this.list])
								}
							}
							this.$db.del('shoppingTrolley_1')
						}
						this.$common.show('保存成功')
						setTimeout(() => {
							this.$common.guanbi('/pages/aDraftList/aDraftList')
						},1000)
					}else {
						this.$common.show('请先添加商品',true)
					}
					
				}else {
					if(this.count == 0) {
						this.arrList =  this.$db.get('dindan')
					}else if(this.count == 1) {
						this.arrList = this.$db.get('dindan_2') // 销货单
					}else {
						this.arrList = this.$db.get('dindan_1') // 退货单
					}
					this.arrList.forEach(e => {
						if(e.dto.externalCode == this.externalCode) {
							if(this.count != 0) {
								e.dto.saleDeliveryDetails = this.SaleOrderDetails
							}else {
								e.dto.saleOrderDetails = this.SaleOrderDetails
							}
						}
					})
					if(this.count == 0) {
						if(this.SaleOrderDetails.length == 0) {
							this.$db.del('dindan')
						}else {
							this.$db.set('dindan',this.arrList)
						}
					}else if(this.count == 1) {
						if(this.SaleOrderDetails.length == 0) {
							this.$db.del('dindan_2')
						}else {
							this.$db.set('dindan_2',this.arrList)
						}
					}else {
						if(this.SaleOrderDetails.length == 0) {
							this.$db.del('dindan_1')
						}else {
							this.$db.set('dindan_1',this.arrList)
						}
					}
					this.$common.show('保存成功')
					setTimeout(() => {
						uni.navigateBack()
					},1000)
				}
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: #F1F0F5;
	}
	.zengClass{
		width: 40rpx;
		height: 40rpx;
		border-radius: 50%;
		background: #FD5960;
	}
</style>